package com.jnshu.quartz;

import org.quartz.*;

import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * @author xmp
 * @date 2019/8/12 23:56
 */
public class MessageUtils {
    /**
     *创建时间
     * @param key id
     * @param cron 时间
     * @param scheduler quartz调度工厂
     * @param map
     * @throws SchedulerException
     */
    public static void create(Long key, String cron, Scheduler scheduler, Map<String,Object> map) throws SchedulerException {
        Long id = (long) map.get("id");
        Integer status = (int) map.get("status");
        JobDataMap jobDataMap=new JobDataMap();
        jobDataMap.put("id",id);
        jobDataMap.put("status",status);
        JobDetail jobDetail = JobBuilder
                .newJob(Mybean.class)
                .withIdentity(String.valueOf(key),"group1")
                .usingJobData(jobDataMap).build();
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity(String.valueOf(key),"group1")
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .build();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("创建事件成功，执行时间为:"+format.format(scheduler.scheduleJob(jobDetail,cronTrigger)));
    }
}
